package cn.sykj.car.controller;


import cn.sykj.car.common.Const;
import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.entity.DictionaryDict;
import cn.sykj.car.entity.Users;
import cn.sykj.car.service.IDictionaryDictService;
import cn.sykj.car.service.IUsersService;
import cn.sykj.car.utils.Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 数据字典dict_group维护（手工维护） 前端控制器
 * </p>
 *
 * @author yixu
 * @since 2021-03-26
 */
@RestController
@Slf4j
@RequestMapping("/dictionaryDict")
public class DictionaryDictController extends BaseController{

    @Resource
    private IDictionaryDictService iDictionaryDictService;

    @Resource
    private IUsersService iUsersService;

    /**
     * 字典维护新增
     * @param dictionaryDict
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "字典维护新增", notes = "字典维护新增")
    @RequiresAuthentication
    @RequiresPermissions("dictionaryDict:create")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseBean addGoodsCategory(@RequestBody DictionaryDict dictionaryDict) throws Exception {

        //获取登录系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);
        //设置创建时间
        dictionaryDict.setCreateTime(new Date());
        //设置修改时间
        dictionaryDict.setUpdateTime(new Date());
        Users userByName=iUsersService.getUserByName((String) obj);
        //设置创建人
        dictionaryDict.setCreateUser(userByName.getUserName());
        //设置修改人
        dictionaryDict.setUpdateUser(userByName.getUserName());

        dictionaryDict.setDelFlag(Const.DEL_FLAG_FALSE);

        Boolean aBoolean = iDictionaryDictService.addDictionaryDict(dictionaryDict);
        this.writeLog("新增字典簇");
        return new ResponseBean(StatusCode.OK,aBoolean);

    }

    /**
     * 修改字典维护
     * @param dictionaryDict
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "修改字典维护", notes = "修改字典维护")
    @RequiresAuthentication
    @RequiresPermissions("dictionaryDict:edit")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseBean updateGoodsCategory(@RequestBody DictionaryDict dictionaryDict) throws Exception {

        //获取登录系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);
        //设置修改时间
        dictionaryDict.setUpdateTime(new Date());

        //设置修改人
        Users userByName=iUsersService.getUserByName((String) obj);
        dictionaryDict.setUpdateUser(userByName.getUserName());

        Boolean aBoolean = iDictionaryDictService.updateDictionaryDict(dictionaryDict);
        this.writeLog("修改字典簇");
        return new ResponseBean(StatusCode.OK,aBoolean);

    }

    /**
     * 删除字典维护字段
     * @param id
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "删除字典维护字段", notes = "删除字典维护字段")
    @RequiresAuthentication
    @RequiresPermissions("dictionaryDict:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public ResponseBean deleteDictionaryDict(@PathVariable Integer id) throws Exception {

        //获取登录系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        Boolean a= iDictionaryDictService.deleteDictionaryDict(id);
        this.writeLog("删除字典簇");
        return new ResponseBean(StatusCode.OK,a);

    }

    /**
     * 查询字典维护字段
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "查询字典维护字段", notes = "查询字典维护字段")
    @RequiresAuthentication
    @RequiresPermissions("dictionaryDict:view")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ResponseBean page(
            @RequestBody DictionaryDict dictionaryDict,
            @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer currentPage,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize) throws Exception {

        //获取登录系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        IPage<DictionaryDict> page = new Page<>(currentPage,pageSize);
        QueryWrapper<DictionaryDict> queryWrapper = new QueryWrapper<>();
        if(Utils.notEmpty(dictionaryDict.getDictValue())){
            queryWrapper.like(DictionaryDict.DICT_VALUE,dictionaryDict.getDictValue());
        }
        if(Utils.notEmpty(dictionaryDict.getDictName())){
            queryWrapper.like(DictionaryDict.DICT_NAME,dictionaryDict.getDictName());
        }
        IPage<DictionaryDict> page1 = iDictionaryDictService.page(page,queryWrapper);

        return new ResponseBean(StatusCode.OK,page1);
    }

}
